//分类页面
var basePage = require('../../common/BasePage');
var baseList = require('../../common/BaseList');
Page({
  behaviors: [basePage, baseList],
  /**
   * 页面的初始数据
   */
  data: {
    reqUrl: '',
    reqData: {
      /**排序[sales:人气最高,sku:库存排序] */
      order: ''
    },
    catIndex: 0, //分类索引
    catData: [],
    /**颜色列表 */
    colorArr: [],
    /**选中的颜色id */
    color_id: 0,
    /**是否显示颜色选择面板 */
    colorShow: false,
    warehouse:{}
  },
  colorBtn_click() {
    this.setData({
      colorShow: true
    })
  },
  cat_change(e) {
    // console.log("cat_change", e);
    this.setData({
      catIndex: e.detail,
      color_id: 0
    });

    this.init_pdc();
  },
  search_click(e) {
    this.data.current_page = 0;
    this.data.reqData = {
      so: e.detail,
      cate_id: this.data.catData[this.data.catIndex].cate_id
    };
    this.req_items();
  },
  order_change(e) {
    this.data.reqData.order = e.currentTarget.dataset.order;
    this.setData({
      reqData: this.data.reqData,
      colorShow: false
    });
    this.data.current_page = 0;
    this.req_items();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function (options) {
    console.log("options", options)
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onLoad: function (options) {

    let warehouse = JSON.parse(options.warehouse);
    this.setData({
      warehouse: warehouse
    })
    wx.setNavigationBarTitle({
      title: '分仓拼单(' + warehouse.title + ")",
    })
    this.data.reqUrl = '/home/wh/product/items?wh_id=' + warehouse.wh_id;
    let catArr = getApp().globalData.cateArr2;
    console.log("catArr", catArr)
    if (!catArr || !catArr.length) { //没有分类缓存
      // 请求分类列表
      return this.request({
        url: this.data.host + '/home/product/cate?limit=1000'
      }, this.get_cate_ok);
    }
    this.setData({
      catData: catArr,
      warehouse: warehouse
    })
    console.log("catID", wx.getStorageSync("catId"))

    if (wx.getStorageSync("catId")) {
      var catId = wx.getStorageSync("catId")
      catArr.map((val, index) => {
        if (val.cate_id == catId) {
          this.setData({
            catIndex: index
          })
        }
      })
    }
    this.init_pdc()

  },
  //渲染分类数据
  get_cate_ok(res) {
    //  console.log("get_cate_ok", res);
    getApp().globalData.cateArr2 = res.data;
    this.setData({
      catData: res.data
    })
    if (res.data && res.data.length) {
      this.init_pdc()
    } else {
      wx.showToast({
        title: '缺少产品数据',
        icon: 'none'
      })
    }


  },

  /**
   * 初始化产品数据
   */
  init_pdc() {
    // console.log("this.data.catIndex",this.data.catIndex)
    let data = this.data;
    // console.log("data.catData",data.catData);
    let order = this.data.reqData.order;
    this.data.reqData = {
      cate_id: data.catData[this.data.catIndex].cate_id,
      color_id: data.color_id,
      so: '',
      order: order
    }
    data.current_page = 0;

    this.req_items();
  },
  /**颜色点击选择点击 */
  color_click(e) {
    console.log("color_click", e);
    this.setData({
      color_id: this.data.colorArr[e.currentTarget.dataset.index].color_id,
      colorShow: false
    });
    this.init_pdc()
  },
  pdc_click(e) {

    let pdc = e.currentTarget.dataset.pdc;

    console.log("pdc", pdc);
    console.log("warehouse", this.data.warehouse);
    wx.navigateTo({
      url: '/pages/cls/pdcList/Index?title=' + pdc.product_name +
        '&product_id=' + pdc.product_id + "&whType=1&wh_id=" + this.data.warehouse.wh_id,
    })
  },
  /**
   * 获得列表数据
   */
  get_items_ok(res) {
    this.data.pages = res.pages;
    this.data.current_page = res.current_page;
    console.log("get_items_ok", res);
    res.data.map((res, index, arr) => {
      this.init_val(res, index, arr)
    })
    let itemArr = this.data.itemArr;
    res.current_page == 1 ? itemArr = res.data : itemArr = itemArr.concat(res.data);
    this.setData({
      itemArr,
      colorArr: res.color_list
    });
    wx.stopPullDownRefresh({
      success: (res) => {},
    })
  },
  init_val(val, index, arr) {
    val.thumb = this.get_thumb(val.product_img);
  },
  //跳转常购清单
  toOften() {
    wx.navigateTo({
      url: '/pages/cls/often/List',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '源花采购',
      path: 'pages/cls/index/index?uid=' + +getApp().globalData.userInfo.uid,
      success: (res) => {
        console.log("转发成功", res);
      },
      fail: (res) => {
        console.log("转发失败", res);
      }
    }
  }
})